package service

import (
	"api-gateway/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
	"path/filepath"
)

func OssUpload(c *gin.Context) {
	file, err := c.FormFile("file")
	if err != nil {
		return
	}
	ext := filepath.Ext(file.Filename)
	filename := "upload/" + file.Filename + ext
	c.SaveUploadedFile(file, "./"+filename)
	wd, _ := os.Getwd()
	url, err := pkg.OssUpload(filename, wd)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "oss上传失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "oss上传成功", "data": gin.H{
		"url": url,
	}})
	return
}
